//模型层 获取数据
import {
  HTTP
} from '../utils/http';

//继承使用HTTP工具里的ajax方法
class CartModel extends HTTP {
  //获取商品列表信息 uid信息，用户id
  getCartList(uid) {
    //利用Promise里resolve写好的字符串和数据结合一起抛出到另外的模块
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/getCartList',
        type: 'POST',
        dataType: 'JSON',
        data: {
          uid
        },
        success: (data) => {
          //抛出data
          resolve(data);
        }
      })
    })
  };
  updateCartNum(id, num) {
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/updateCartNum',
        type: 'POST',
        dataType: 'JSON',
        data: {
          id: id,
          num: num
        },
        success: (data) => {
          //抛出data
          resolve(data);
        }
      })
    })
  };
  removeCartItem(id) {
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/removeCartItem',
        type: 'POST',
        dataType: 'JSON',
        data: {
          id: id
        },
        success: (data) => {
          //抛出data
          resolve(data);
          //{msg: 'ok', msg_code: '200'}
        }
      })
    })
  };
  purchaseCart(uid, gids) {
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/purchaseCart',
        type: 'POST',
        dataType: 'JSON',
        data: {
          uid: uid,
          gids: gids
        },
        success: (data) => {
          //抛出data
          resolve(data);
          //{msg: 'ok', msg_code: '200'}
        }
      })
    })
  }
}

//导出模块
export {
  CartModel
};